package com.cino.demos.sourceCode.AOPdemo;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

public class AopInvocationHandler implements InvocationHandler {
    private final Object target; // 真实对象
    private final List<MethodInterceptor> interceptors; // 拦截器链
    
    public AopInvocationHandler(Object target, List<MethodInterceptor> interceptors) {
        this.target = target;
        this.interceptors = interceptors;
    }
    
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("\n🚀 拦截到方法调用: " + method.getName() + 
                          ", 参数: " + Arrays.toString(args));
        
        // 处理Object方法
        if (method.getDeclaringClass() == Object.class) {
            return method.invoke(this, args);
        }
        
        // 创建方法调用对象
        MethodInvocation invocation = new MyMethodInvocation(
            proxy, target, method, args, interceptors
        );
        
        // 启动拦截器链执行 ⭐
        System.out.println("  🔗 启动拦截器链执行");
        return invocation.proceed();
    }
}